<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    width="900px"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { dataFormSchema } from './menu.data';
  import {
    postSysMenuApi,
    getSysMenuAndExcludeNodeWithOptionTreeApi,
  } from '/@/api/modules/system/menu';

  const emit = defineEmits(['success', 'register']);

  const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));
  const isUpdate = ref(false);
  const rowId = ref();

  const { createMessage } = useMessage();
  const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: dataFormSchema,
    showActionButtonGroup: false,
    baseColProps: { lg: 12, md: 24 },
    actionColOptions: {
      span: 23,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    const treeData = await getSysMenuAndExcludeNodeWithOptionTreeApi({ nodeId: data.record.id });
    await updateSchema([
      {
        field: 'parentId',
        componentProps: { treeData },
      },
    ]);

    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      // const resData = await viewObj({ id: data.record.id });
      await setFieldsValue({
        ...data.record,
        // 设置默认值
        parentId: data.record.parentId === '0' ? null : data.record.parentId,
        visible: !data.record.visible ? '0' : data.record.visible,
        keepalive: !data.record.keepalive ? '0' : data.record.keepalive,
        linkExternal: !data.record.linkExternal ? '1' : data.record.linkExternal,
        frame: !data.record.frame ? '0' : data.record.frame,
      });
    } else {
      await setFieldsValue({
        parentId: data.record.parentId,
      });
    }
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      !unref(isUpdate)
        ? await postSysMenuApi({ ...values })
        : await postSysMenuApi({ ...values, id: rowId.value });
      !unref(isUpdate) ? createMessage.success('新增成功！') : createMessage.success('编辑成功！');
      closeModal();
      emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
